<?php
namespace App\Http\Controllers\Api;
use App\Models\Collect;
use App\Models\Feedback;
use App\Models\Member;
use Illuminate\Support\Facades\DB;
class MineController extends BaseController
{
    public function feedback(){
        try {
            $userinfo=$this->userInfo;
            $param=request()->all();
            $validator = \Validator::make($param, [
                'content'   => 'required|max:300',
                'client'   => 'required|in:1,2',
                'version'   => 'required',
            ]);
            $error = $validator->errors()->first();
            if ($error)
                appMessage(1,$error);
            $data=[
                'user_phone'=>$userinfo['phone'],
                'content'=>$param['content'],
                'contact'=>$param['contact'],
                'client'=>$param['client'],
                'version'=>$param['version']
            ];
            if (Feedback::create($data)){
                appMessage(0,'操作成功');
            }else{
                appMessage(1,'操作失败');
            }
        } catch (\Exception $e) {
            appMessage(1,$e->getMessage());
        }
    }
    public function addCollect(){
        try {
            $userinfo=$this->userInfo;
            $param=request()->all();
            $validator = \Validator::make($param, [
                'goods_id'   => 'required|exists:goods,id'
            ]);
            $error = $validator->errors()->first();
            if ($error)
                appMessage(1,$error);
            $data=[
                'member_id'=>$userinfo['id'],
                'goods_id'=>$param['goods_id']
            ];
            if (Collect::create($data)){
                appMessage(0,'操作成功');
            }else{
                appMessage(1,'操作失败');
            }
        } catch (\Exception $e) {
            appMessage(1,$e->getMessage());
        }
    }
    public function removeCollect(){
        try {
            $userinfo=$this->userInfo;
            $param=request()->all();
            $validator = \Validator::make($param, [
                'goods_id'   => 'required|exists:goods,id'
            ]);
            $error = $validator->errors()->first();
            if ($error)
                appMessage(1,$error);
            $collect = Collect::query()
                ->where('member_id','=',$userinfo['id'])
                ->where('goods_id','=',$param['goods_id'])
                ->first();
            if ($collect->delete()){
                appMessage(0,'操作成功');
            }else{
                appMessage(1,'操作失败');
            }
        } catch (\Exception $e) {
            appMessage(1,$e->getMessage());
        }
    }
    public function collects(){
        $param = request()->all();
        $validator = \Validator::make($param, [     //正则验证
            'page' => 'integer',
            'pageSize' => 'integer'
        ]);
        $error = $validator->errors()->first();
        if ($error)
            appMessage(1,$error);
        $pageSize = empty($param["pageSize"]) ? 20 : $param["pageSize"];
        try {
            $userinfo=$this->userInfo;

            $collectModel =  Collect::query()
                ->leftJoin('goods', function ($join) {
                    $join->on('goods.id', '=', 'collect.goods_id');
                });
            $collect = $collectModel
                ->select(['goods.id','goods.goods_name','goods.pic_url','goods.shelf_status','goods.sell_price','goods.actual_price','goods.tao_sale','goods.coupon_amount'])
                ->where('collect.member_id','=',$userinfo['id'])
                ->orderBy('collect.id','desc')
                ->paginate($pageSize)
                ->toArray();
            appMessage(0,'ok',$collect['data']);
        } catch (\Exception $e) {
            appMessage(1,$e->getMessage());
        }
    }

    public function setAvatar()
    {
        $param = request()->all();
        $validator = \Validator::make($param, [     //正则验证
            'avatar' => 'required|string|max:255'
        ]);
        $error = $validator->errors()->first();
        if ($error)
            appMessage(1,$error);
        try {
            $userinfo=$this->userInfo;
            $member = Member::findOrFail($userinfo['id']);
            $data=['avatar'=>$param['avatar']];
            if ($member->update($data)){
                appMessage(0,'ok');
            }
            appMessage(1,'操作失败');
        } catch (\Exception $e) {
            appMessage(1,$e->getMessage());
        }
    }
    public function setSearchHistory(){
        $param = request()->all();
        $validator = \Validator::make($param, [     //正则验证
            'key_words' => 'string|max:300',
        ]);
        $error = $validator->errors()->first();
        if ($error)
            appMessage(1,$error);
        try {
            $userinfo=$this->userInfo;
            if(isset($param['key_words'])){
                $key_words=$param['key_words'];
                $key_words_arr=explode(',',$key_words);
                $key_words_arr=array_unique($key_words_arr);
                if(count($key_words_arr)>12)
                    $key_words_arr=array_slice($key_words_arr,0,12);
                $key_words=implode(',',$key_words_arr);

                $search=DB::table("search")
                    ->where(['member_id' => $userinfo['id']])
                    ->first();
                if(empty($search)){
                    unset($search);
                    $bool= DB::table('search')->insert([
                        'member_id'=>$userinfo['id'],
                        'key_words' => $key_words,
                    ]);
                    if($bool)
                        appMessage(0,'ok');
                }else{
                    $num =DB::table("search")
                        ->where('id', $search->id)
                        ->update(['key_words'=>$key_words]);
                    if($num)
                        appMessage(0,'ok');
                }
            }else{
                $num=DB::table("search")
                    ->where(['member_id' => $userinfo['id']])
                    ->delete();
                if($num)
                    appMessage(0,'ok');
            }
            appMessage(1,'设置失败');
        }catch (\Exception $e) {
            appMessage(1,$e->getMessage());
        }
    }
}